package cn.taokuaidi.wangwang.client.widget.grid;

import java.util.ArrayList;
import java.util.List;

import cn.taokuaidi.wangwang.client.context.BuyerDataEventContext;
import cn.taokuaidi.wangwang.client.context.BuyerGridSelectEventContext;
import cn.taokuaidi.wangwang.client.model.OrderDO;
import cn.taokuaidi.wangwang.client.widget.form.ExpressForm;

import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.RowExpander;
import com.google.gwt.user.client.Event;

/**
 * @author 嘉陵江里捉鱼 email:tang7259@163.com
 *@version 1.0
 *@time Nov 3, 2008 10:37:50 AM
 */
public class BuyerGrid extends TkdGrid {

    private ExpressForm expressForm;

    private native String getBasicTemplate() /*-{ 
                                  return ['{detail}'].join(""); 
                                  }-*/;

    public void initData() {
        BuyerDataEventContext.load();
        ListStore<OrderDO> store = BuyerDataEventContext.getStore();

        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

        XTemplate tpl = XTemplate.create(getBasicTemplate());

        RowExpander expander = new RowExpander();
        expander.setTemplate(tpl);

        configs.add(expander);

        ColumnConfig seller_nick_image = new ColumnConfig();
        seller_nick_image.setId("seller_nick");
        seller_nick_image.setHeader("");
        seller_nick_image.setWidth(0);
        seller_nick_image.setHidden(true);
        configs.add(seller_nick_image);

        ColumnConfig seller_nick = new ColumnConfig();
        seller_nick.setId("seller_nick_msg");
        seller_nick.setHeader("卖家");
        seller_nick.setWidth(200);
        configs.add(seller_nick);

        ColumnConfig express = new ColumnConfig();
        express.setId("express_info");
        express.setHeader("快递信息");
        express.setWidth(150);
        configs.add(express);

        ColumnConfig status = new ColumnConfig();
        status.setId("last_status");
        status.setHeader("状态");
        status.setWidth(200);
        configs.add(status);

        ColumnConfig title = new ColumnConfig();
        title.setId("title");
        title.setHeader("商品");
        title.setWidth(200);
        configs.add(title);

        ColumnConfig num = new ColumnConfig();
        num.setId("num");
        num.setHeader("数目");
        num.setWidth(50);
        configs.add(num);

        ColumnConfig price = new ColumnConfig();
        price.setId("price");
        price.setHeader("单价(￥)");
        price.setWidth(100);
        configs.add(price);

        ColumnConfig total = new ColumnConfig();
        total.setId("total");
        total.setHeader("总额(￥)");
        total.setWidth(100);
        configs.add(total);

        cm = new ColumnModel(configs);
        this.reconfigure(store, cm);
        this.addPlugin(expander);
        this.addListener(Event.ONDBLCLICK, new Listener<GridEvent>() {

            public void handleEvent(GridEvent be) {
                if (be.rowIndex >= 0) {
                    List<OrderDO> listOrder = (List<OrderDO>) be.grid.getModel();
                    OrderDO order = (OrderDO) getStore().getAt(be.rowIndex);
                    BuyerGridSelectEventContext.init(order);
                    expressForm.loadOrder(false, order);
                    expressForm.show();
                }
            }

        });
        this.setLoadMask(true);
        this.setBorders(true);

    }

    public BuyerGrid(ListStore store, ColumnModel cm) {
        super(store, cm);
        expressForm = new ExpressForm();

    }

}
